﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Tetris
{
    public class ZPiece : Piece
    {
        public ZPiece()
            : base(new List<Square> { new Square(3, 1), new Square(4, 1, true), new Square(4, 2), new Square(5, 2) })
        {
            SetColors();
        }

        public override void SetPieceLocationsForGrid()
        {
            squares[0].gridX = 3;
            squares[0].gridY = 1;

            squares[1].gridX = 4;
            squares[1].gridY = 1;

            squares[2].gridX = 4;
            squares[2].gridY = 2;

            squares[3].gridX = 5;
            squares[3].gridY = 2;
        }

        public override void SetPieceLocationsForHoldPiece()
        {
            squares[0].gridX = 1;
            squares[0].gridY = 1;

            squares[1].gridX = 2;
            squares[1].gridY = 1;

            squares[2].gridX = 2;
            squares[2].gridY = 2;

            squares[3].gridX = 3;
            squares[3].gridY = 2;
        }

        public override void SetColors()
        {
            foreach (Square square in squares)
            {
                square.Color = Color.White;
                square.backgroundString = "Images/red_block";
            }
            //squares[0].Color = Color.Red;
            //squares[1].Color = Color.Red;
            //squares[2].Color = Color.Red;
            //squares[3].Color = Color.Red;
        }

        public override void RotateRight()
        {
            previousPiece = this;
            if (Direction == Directions.North)
                base.RotateRight();
            else if (Direction == Directions.East)
            {
                Square pivotSquare = squares.Where(x => x.isPivot == true).FirstOrDefault();
                foreach (Square square in squares)
                {
                    if (!square.isPivot)
                    {
                        square.gridX -= pivotSquare.gridX;
                        square.gridY -= pivotSquare.gridY;

                        //Rotate
                        int tempX = square.gridX;
                        square.gridX = square.gridY;
                        square.gridY = tempX * -1;

                        //Add back pivot square relational values for final location on grid
                        square.gridX += pivotSquare.gridX;
                        square.gridY += pivotSquare.gridY;
                    }
                }
                Direction = Directions.North;
                CheckForValidRotation();
            }
        }
    }
}
